package susebs.pb.travel_server.service.impl;

import susebs.pb.travel_server.entity.HotelPackage;
import susebs.pb.travel_server.dao.HotelPackageDao;
import susebs.pb.travel_server.entity.Hotels;
import susebs.pb.travel_server.entity.Landmarks;
import susebs.pb.travel_server.service.HotelPackageService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import susebs.pb.travel_server.service.HotelsService;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * (HotelPackage)表服务实现类
 *
 * @author makejava
 * @since 2024-02-12 16:56:22
 */
@Service("hotelPackageService")
public class HotelPackageServiceImpl implements HotelPackageService {
    @Resource
    private HotelPackageDao hotelPackageDao;
    @Resource
    private HotelsService hotelsService;

    /**
     * 通过ID查询单条数据
     *
     * @param hotelId 主键
     * @return 实例对象
     */
    @Override
    public HotelPackage queryById(Long hotelId) {
        return this.hotelPackageDao.queryById(hotelId);
    }

    /**
     * 分页查询
     *
     * @param hotelPackage 筛选条件
     * @param pageRequest  分页对象
     * @return 查询结果
     */
    @Override
    public Page<HotelPackage> queryByPage(HotelPackage hotelPackage, PageRequest pageRequest) {
        long total = this.hotelPackageDao.count(hotelPackage);
        return new PageImpl<>(this.hotelPackageDao.queryAllByLimit(hotelPackage, pageRequest), pageRequest, total);
    }

    /**
     * 套餐关联的实体id列表
     */
    @Override
    public ArrayList queryAll(Long pkId) {
        ArrayList<Long> res = new ArrayList<>();

        ArrayList<HotelPackage> list = hotelPackageDao.queryAll(pkId);
        for (HotelPackage h: list) {
            res.add(h.getHotelId());
        }

        return res;
    }

    public ArrayList<Hotels> queryByIds(ArrayList<Long> list) {
        ArrayList<Hotels> res = new ArrayList<>();
        for (Long id:list) {
            Hotels hotel = hotelsService.queryById(id);
            res.add(hotel);
        }
        return res;
    }

    /**
     * 新增数据
     *
     * @param hotelPackage 实例对象
     * @return 实例对象
     */
    @Override
    public HotelPackage insert(HotelPackage hotelPackage) {
        this.hotelPackageDao.insert(hotelPackage);
        return hotelPackage;
    }

    /**
     * 修改数据
     *
     * @param hotelPackage 实例对象
     * @return 实例对象
     */
    @Override
    public HotelPackage update(HotelPackage hotelPackage) {
        this.hotelPackageDao.update(hotelPackage);
        return this.queryById(hotelPackage.getHotelId());
    }

    /**
     * 通过主键删除数据
     *
     * @param hotelId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long hotelId) {
        return this.hotelPackageDao.deleteById(hotelId) > 0;
    }
}
